package com.yjy.fuyao.stu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjy.fuyao.stu.client.OrderClient;
import com.yjy.fuyao.stu.entity.Course;
import com.yjy.fuyao.stu.entity.form.CourseInfo;
import com.yjy.fuyao.stu.entity.form.CommentInfo;
import com.yjy.fuyao.stu.entity.vo.ChapterVo;
import com.yjy.fuyao.stu.entity.vo.CourseQueryVo;
import com.yjy.fuyao.stu.entity.vo.CourseWebVo;
import com.yjy.fuyao.stu.entity.vo.RateCourseVo;
import com.yjy.fuyao.stu.service.ChapterService;
import com.yjy.fuyao.stu.service.CourseCollectService;
import com.yjy.fuyao.stu.service.CourseService;
import com.yjy.fuyao.utils.result.R;
import com.yjy.fuyao.utils.result.ResponseEnum;
import com.yjy.fuyao.utils.util.JwtUtils;
import com.yjy.fuyao.utils.vo.CourseInfoVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 前台课程功能控制器
 */
@RestController
@RequestMapping("/front/stu/course")
public class CourseController {

    @Resource
    private CourseService courseService;

    @Resource
    private CourseCollectService courseCollectService;

    @Resource
    private ChapterService chapterService;

    @Resource
    private OrderClient orderClient;

    @ApiOperation(value = "分页课程列表")
    @PostMapping(value = "/list/{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVo courseQuery){
        Page<Course> pageParam = new Page<Course>(page, limit);
        Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
        return  R.ok().data(map);
    }

    @ApiOperation(value = "分页收藏课程列表")
    @GetMapping(value = "/collect/{page}/{limit}")
    public R pageCollect(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            HttpServletRequest request){
        String userId = JwtUtils.getUserIdByJwtToken(request);
        if(userId == ""){
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
        Map<String, Object> map = courseService.pageCollectWeb(page,limit,userId);
        return  R.ok().data(map);
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "/get/{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId, HttpServletRequest request){
        //更新课程浏览次数
        courseService.updatePageViewCount(courseId);
        //课程查询课程基本信息
        CourseWebVo courseFrontInfo = courseService.selectInfoWebById(courseId);
        //查询课程里面大纲数据
        List<ChapterVo> chapterVideoList = chapterService.nestedList(courseId);

        //远程调用，判断课程是否被购买
        boolean buyCourse = false;
        String userId = JwtUtils.getUserIdByJwtToken(request);
        if(userId != ""){
            buyCourse = orderClient.isBuyCourse(userId, courseId);
        }
        boolean starCourse = false;
        if (userId != ""){
            starCourse = courseCollectService.isStarCourse(userId, courseId);
        }
        return R.ok()
                .data("courseFrontInfo",courseFrontInfo)
                .data("chapterVideoList",chapterVideoList)
                .data("isbuy",buyCourse)
                .data("isStar",starCourse);
    }

    //根据课程id查询课程信息
    @GetMapping("getDto/{courseId}")
    public CourseInfoVo getCourseInfoDto(@PathVariable String courseId) {
        CourseInfo courseInfoForm = courseService.getCourseInfo(courseId);
        CourseInfoVo courseInfo = new CourseInfoVo();
        BeanUtils.copyProperties(courseInfoForm,courseInfo);
        return courseInfo;
    }

    //为课程打分
    @PostMapping("rate")
    public R rateForCourse(@RequestBody CommentInfo commentInfo) {
        RateCourseVo rateCourseVo = courseService.rateForCourse(commentInfo);
        return R.ok().data("rateCourseInfo",rateCourseVo);
    }

}

